/**
 * Copyright (c) 2002 qc&Bull Co.,LTD
 * All right reserved.
 */
package cn.zxd.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * <p>ClassName: JdbcUrl </p>
 * <p>Description: 构建 Jdbc 的 url </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: qc&Bull </p>
 * @author: HST
 * @version: 1.0
 * @date: 2002-05-31
 */
public class JdbcUrl {
    // @Field
    private String DBType;
    private String IP;
    private String Port;
    private String DBName;
    private String ServerName;
    private String UserName;
    private String PassWord;
    // @Constructor
    public JdbcUrl() {
        config();
        //三星测试
//	    DBType = "ORACLE";
//	    IP = "30.1.20.39";
//	    Port = "1521";
//	    DBName = "jkxjk";
//	    UserName = "jiangsu_secjkxjk";
//	    PassWord = "jiangsu_secjkxjk";


        //信诚测试
//	    DBType = "ORACLE";
//	    IP = " 128.232.9.224";
//	    Port = "1521";
//	    DBName = "cfnl";
//	    UserName = "jsbj";
//	    PassWord = "password";
//	    //中英
//    	DBType = "DB2";
//	    IP = "10.146.33.130";
//	    Port = "50000";
//	    DBName = "jszjpt";
//	    UserName = "db2admin";
//	    PassWord = "db2admin";
//    	czl

        //江苏中介
//    	DBType = "ORACLE";
//	    IP = "10.3.231.176";
//	    Port = "1521";
//	    DBName = "orcl";
//	    UserName = "life";
//	    PassWord = "life";

//    	DBType = "ORACLE";
//	    IP = "127.0.0.1";
//	    Port = "1521";
//	    DBName = "orcl";
//	    UserName = "life3";
//	    PassWord = "life3";

//    	DBType = "ORACLE";
//	    IP = "10.11.61.19";
//	    Port = "1521";
//	    DBName = "orcl";
//	    UserName = "life";
//	    PassWord = "life";

//    	DBType = "ORACLE";
//	    IP = "30.1.10.90";
//	    Port = "1521";
//	    DBName = "orcl";
//	    UserName = "iiep";
//	    PassWord = "iiep";

//        DBType = "WEBLOGICPOOL";
//        DBName = "prip_jndi";
    }

    // @Method
    public String getDBType() {
        return DBType;
    }

    public String getIP() {
        return IP;
    }

    public String getPort() {
        return Port;
    }

    public String getDBName() {
        return DBName;
    }

    public String getServerName() {
        return ServerName;
    }

    public String getUserName() {
        return UserName;
    }

    public String getPassWord() {
        return PassWord;
    }

    public void setDBType(String aDBType) {
        DBType = aDBType;
    }

    public void setIP(String aIP) {
        IP = aIP;
    }

    public void setPort(String aPort) {
        Port = aPort;
    }

    public void setDBName(String aDBName) {
        DBName = aDBName;
    }

    public void setServerName(String aServerName) {
        ServerName = aServerName;
    }

    public void setUser(String aUserName) {
        UserName = aUserName;
    }

    public void setPassWord(String aPassWord) {
        PassWord = aPassWord;
    }

    /**
     * 获取连接句柄
     * @return String
     */
    public String getJdbcUrl() {
        String sUrl = "";

        if (DBType.trim().toUpperCase().equals("ORACLE")) {
            sUrl = "jdbc:oracle:thin:@" + IP + ":"
                    + Port + ":"
                    + DBName;
        }

        if (DBType.trim().toUpperCase().equals("INFORMIX")) {
            sUrl = "jdbc:informix-sqli://" + IP + ":"
                    + Port + "/"
                    + DBName + ":"
                    + "informixserver=" + ServerName + ";"
                    + "user=" + UserName + ";"
                    + "password=" + PassWord + ";";
        }

//        if (DBType.trim().toUpperCase().equals("SQLSERVER")) {
//            sUrl = "jdbc:inetdae:" + IP + ":"
//                   + Port + "?sql7=true&"
//                   + "database=" + DBName + "&"
//                   + "charset=gbk";
//        }
//        if (DBType.trim().toUpperCase().equals("SQLSERVER_OTHER")) {
//            sUrl = "jdbc:sqlserver://" + IP + ":"
//                   + Port + "; DatabaseName=" + DBName;
//        }
        if (DBType.trim().toUpperCase().equals("SQLSERVER_OTHER")) {
       /*     sUrl = "jdbc:inetdae:" + IP + ":"
                   + Port + "?sql7=true&"
                   + "database=" + DBName + "&"
                   + "charset=gbk";*/
            sUrl = "jdbc:sqlserver://" + IP + ";"
                    + "database=" + DBName ;
        }
        if (DBType.trim().toUpperCase().equals("SQLSERVER")) {
            sUrl = "jdbc:sqlserver://" + IP + ":"
                    + Port + "; DatabaseName=" + DBName;
        }
        if (DBType.trim().toUpperCase().equals("WEBLOGICPOOL")) {
            sUrl = "jdbc:weblogic:pool:" + DBName;
        }

        if (DBType.trim().toUpperCase().equals("DB2")) {
            sUrl = "jdbc:db2://" + IP + ":"
                    + Port + "/"
                    + DBName;
        }
        if (DBType.trim().toUpperCase().equals("AS400")) {
            sUrl = "jdbc:as400://" + IP
                    + "/"+ DBName ;
        }
        return sUrl;
    }


    protected static Properties dbcfg = null;

    public static void loadDBCfg()
    {
        dbcfg = new Properties();
        InputStream in = JdbcUrl.class.getClassLoader().getResourceAsStream("dbcfg.properties");
        try
        {
            dbcfg.load(in);
            System.out.println(" LOAD dbcfg.properties SUCCESS! ");
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.out.println("LOAD dbcfg.properties FAIL!!!");
            dbcfg=null;
        }
    }

    static
    {
        if (dbcfg == null)
        {
            loadDBCfg();
        }
    }

    protected void config(){
        String localConnType = dbcfg.getProperty("LocalConnType");
        if("DBCP".equals(localConnType)){
            this.DBType = dbcfg.getProperty("DBType_");
            this.DBName = dbcfg.getProperty("DBName_");
        }else{
            this.DBType = dbcfg.getProperty("DBType");
            this.IP = dbcfg.getProperty("IP");
            this.Port = dbcfg.getProperty("Port");
            this.DBName = dbcfg.getProperty("DBName");
            this.ServerName = dbcfg.getProperty("ServerName");
            this.UserName = dbcfg.getProperty("UserName");
            this.PassWord = dbcfg.getProperty("PassWord");
        }

    }
}
